/**
 * Created by Jerry on 2017/10/25.
 */

/*
* 提示与加载工具类
* */
export default class Tips {
  static isLoading = false
  static pause = false

  /*
   * 弹出提示框
   * */
  static success(title, duration = 500) {
    wx.showToast({
      title: title,
      icon: 'success',
      mask: true,
      duration: duration
    })
    if (duration > 0) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve()
        }, duration)
      })
    }
  }

  /*
   * 弹出确认窗口
   * isShow false时，无取消
   * payload 为确认的回调
   * */
  static modal(text, isShow, payload, title = '提示') {
    if (typeof isShow === 'string') title = isShow
    if (typeof isShow === 'function') payload = isShow
    return new Promise((resolve, reject) => {
      wx.showModal({
        title: title,
        content: text,
        showCancel: (typeof isShow === 'boolean') ? isShow : true,
        success: res => {
          if (res.confirm) {
            resolve(payload)
          } else if (res.cancel) {

          }
          resovle(res)
        },
        fail: res => {
          reject(res)
        }
      })
    })
  }

  /*
   *toast提示
   * */
  static toast(title, icon = 'success') {
    wx.showToast({
      title: title,
      icon: icon,
      mask: true,
      duration: 2000
    })
  }


  /*
   * 弹出加载提示loading
   * */
  static loading(title = '加载中') {
    if (this.isLoading) return
    this.isLoading = true
    if (wx.showLoading) {
      wx.showLoading({
        title: title,
        mask: true,
      })
    } else {
      wx.showNavigationBarLoading()
    }
  }

  /*
  * 关闭加载提示loading
  * */
  static hideLoading(){
    if(!this.isLoading) return
    this.isLoading=false
    if(wx.hideLoading){
      wx.hideLoading()
    }
  }

  /**
   * 弹出下拉动作栏
   */
  static action(...items) {
    return new Promise((resolve, reject) => {
      wx.showActionSheet({
        itemList: items,
        success: function (res) {
          const result = {
            index: res.tapIndex,
            text: items[res.tapIndex]
          };
          resolve(result);
        },
        fail: function (res) {
          reject(res.errMsg);
        }
      });
    });
  }

  /*
   *分享
   */
  static share(title, url, desc) {
    return {
      title: title,
      path: url,
      desc: desc,
      success: function (res) {
        Tips.toast('分享成功');
      }
    };


  }

}



